package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.clues.domain.TbActivity;
import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.dto.ActivityUpdateDTO;
import com.huike.clues.domain.dto.TbActivityAddDTO;
import com.huike.clues.domain.dto.TbActivityPageDto;
import com.huike.clues.domain.vo.ActivityEchoVo;
import com.huike.clues.mapper.TbActivityMapper;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.service.TbActivityService;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 93238
 * @description 针对表【tb_activity(活动管理)】的数据库操作Service实现
 * @createDate 2023-10-12 06:35:46
 */
@Service
@RequiredArgsConstructor
public class TbActivityServiceImpl extends ServiceImpl<TbActivityMapper, TbActivity>
        implements TbActivityService {

    private final TbActivityMapper tbActivityMapper;
    private final TbClueMapper tbClueMapper;

    /**
     * @author lxc
     */
    @Override
    public void insertActivity(TbActivityAddDTO tbActivityAddDTO) {
        TbActivity tbActivity = new TbActivity();
        BeanUtils.copyProperties(tbActivityAddDTO, tbActivity);
        tbActivity.setCreateTime(LocalDateTime.now().toString());
        tbActivityMapper.insert(tbActivity);
    }

    /**
     * @author lxc
     */
    @Override
    public Page<TbActivity> findActivityList(TbActivityPageDto tbActivityPageDto, LocalDate beginCreateTime,
                                             LocalDate endCreateTime, LocalDate beginTime, LocalDate endTime) {
        Page<TbActivity> page = new Page<>(tbActivityPageDto.getPageNum(), tbActivityPageDto.getPageSize());
        LambdaQueryWrapper<TbActivity> Wrapper = new LambdaQueryWrapper<>();
        Wrapper.like(StringUtils.isNotBlank(tbActivityPageDto.getCode()), TbActivity::getCode, tbActivityPageDto.getCode())
                .eq(tbActivityPageDto.getChannel() != null, TbActivity::getChannel, tbActivityPageDto.getChannel())
                .ge(beginCreateTime != null, TbActivity::getCreateTime, beginCreateTime)
                .le(endCreateTime != null, TbActivity::getCreateTime, endCreateTime)
                .ge(beginTime != null, TbActivity::getBeginTime, beginTime)
                .le(endTime != null, TbActivity::getEndTime, endTime);
        Page<TbActivity> tbActivityPage = tbActivityMapper.selectPage(page, Wrapper);
        return tbActivityPage;
    }

    /**
     * @author lxc
     */
    @Override
    public ActivityEchoVo findActivityById(Long id) {
        TbActivity tbActivity = tbActivityMapper.selectById(id);
        if (tbActivity == null) {
            throw new BaseException("查不到活动信息");
        }
        // LambdaQueryWrapper<TbClue> wrapper = new LambdaQueryWrapper<>();
        // wrapper.eq(TbClue::getActivityId, id);
        // TbClue tbClue = tbClueMapper.selectOne(wrapper);
        // if(tbClue == null){
        //     throw new BaseException("查不到线索信息");
        // }
        ActivityEchoVo activityEchoVo = new ActivityEchoVo();
        BeanUtils.copyProperties(tbActivity, activityEchoVo);
        // activityEchoVo.setUpdateBy(SecurityUtils.getUsername());
        // activityEchoVo.setUpdateTime(LocalDate.now());
        return activityEchoVo;
    }

    /**
     * @author lxc
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateActivity(ActivityUpdateDTO activityUpdateDTO) {
        TbActivity tbActivity = new TbActivity();
        BeanUtils.copyProperties(activityUpdateDTO, tbActivity);
        ArrayList<String> list = new ArrayList<>();
        list.add(activityUpdateDTO.getBeginTime());
        list.add(activityUpdateDTO.getEndTime());
        tbActivityMapper.updateById(tbActivity);
    }

    /**
     * @author lxc
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteActivity(List<Long> ids) {
        LambdaQueryWrapper<TbClue> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(ids != null, TbClue::getActivityId, ids);
        List<TbClue> tbClues = tbClueMapper.selectList(wrapper);
        for (TbClue tbClue : tbClues) {
            if(tbClue != null){
                throw new BaseException("活动与线索有关联,无法删除");
            }
        }

        tbActivityMapper.deleteBatchIds(ids);
    }

    @Override
    public List<ActivityEchoVo> activityListSelect(String channel) {
        List<ActivityEchoVo> list = new ArrayList<>();
        LambdaQueryWrapper<TbActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(channel), TbActivity::getChannel, channel);
        List<TbActivity> tbActivities = tbActivityMapper.selectList(wrapper);
        if(tbActivities == null){
            throw new BaseException("没有查找到活动数据");
        }
        for (TbActivity tbActivity : tbActivities) {
            ActivityEchoVo activityEchoVo = new ActivityEchoVo();
            BeanUtils.copyProperties(tbActivity, activityEchoVo);
            list.add(activityEchoVo);
        }

        return list;
    }
}




